//
//  CYAudio.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/21.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYAudio.h"
#import <AudioToolbox/AudioToolbox.h>

@interface CYAudioManager()
@end

@implementation CYAudioManager
static CYAudioManager *_shareCYAudioManager;
static dispatch_once_t onceToken;
+ (instancetype)shareCYAudioManager{
    if (!_shareCYAudioManager) {
        dispatch_once(&onceToken, ^{
            _shareCYAudioManager = [[CYAudioManager alloc] init];
        });
    }
    return _shareCYAudioManager;
}

- (void)setOpenSound:(BOOL)openSound{
    if (_openSound != openSound){
        _openSound = openSound;
        NSString *openSoundStr = (openSound)?@"YES":@"NO";
        [CYCache saveData:openSoundStr forKey:@"openSound" synchronize:YES];//把声音开关保存到本地
        //        [[UIApplication sharedApplication] setIdleTimerDisabled:YES];   // 设置播放时屏幕常亮
        
    }
}

- (void)setOpenVibrate:(BOOL)openVibrate{
    if (_openVibrate != openVibrate) {
        _openVibrate = openVibrate;
        NSString *openVibrateStr = (openVibrate)?@"YES":@"NO";
        [CYCache saveData:openVibrateStr forKey:@"openVibrate" synchronize:YES];//把震动开关保存到本地
        //        [[UIApplication sharedApplication] setIdleTimerDisabled:YES];   // 设置播放时屏幕常亮
        
    }
}
- (void)setOpenAlert:(BOOL)openAlert{
    if (_openAlert != openAlert) {
        _openAlert = openAlert;
        NSString *openAlertStr = (openAlert)?@"YES":@"NO";
        [CYCache saveData:openAlertStr forKey:@"openAlert" synchronize:YES];
        //        [[UIApplication sharedApplication] setIdleTimerDisabled:YES];   // 设置播放时屏幕常亮
    }
}
/**
 *@pragma 初始化开关属性
 */
+ (void)initializeOpen{
    CYAudioManager *cy_audioManager = [CYAudioManager shareCYAudioManager];
    NSString *openSound = [CYCache readDataWithKey:@"openSound"];
    NSString *openVibrate = [CYCache readDataWithKey:@"openVibrate"];
    NSString *openAlert = [CYCache readDataWithKey:@"openAlert"];
    cy_audioManager.openSound = (openSound && [openSound containsString:@"YES"])?YES:NO;
    cy_audioManager.openVibrate = (openVibrate && [openVibrate containsString:@"YES"])?YES:NO;
    cy_audioManager.openAlert = (openAlert && [openAlert containsString:@"YES"])?YES:NO;
}
@end



@interface CYAudio()
{
    SystemSoundID _soundID;
}
@end
@implementation CYAudio

//播放自定义声音，需要在工程资源里
+ (void)playSoundWithResourceName:(NSString *)resourceName ofType:(NSString *)type{
    if ([[CYAudioManager shareCYAudioManager] openSound]) {
        SystemSoundID currentSoundID;
        NSString *path = [[NSBundle mainBundle] pathForResource:resourceName ofType:type];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:path]), &currentSoundID);
        [self playSound:currentSoundID];
    }
}

//播放自定义声音
+ (void)playSoundWithResource:(NSString *)resource{
    if ([[CYAudioManager shareCYAudioManager] openSound]) {
        SystemSoundID currentSoundID;
        NSURL *url = [[NSBundle mainBundle] URLForResource:resource withExtension:nil];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &currentSoundID);
        [self playSound:currentSoundID];
    }
}

//播放震动
+ (void)playVibrate{
    if ([[CYAudioManager shareCYAudioManager] openVibrate]) {
        [self playSound:kSystemSoundID_Vibrate];
        
    }
}

//播放系统预定义的声音
+ (void)playSystemSoundID:(NSInteger)systemSoundID{
    if ([[CYAudioManager shareCYAudioManager] openSound])
        [self playSound:(UInt32)systemSoundID];
}


//播放提醒
+ (void)playAlertWithResourceName:(NSString *)resourceName ofType:(NSString *)type{
    if ([[CYAudioManager shareCYAudioManager] openAlert]) {
        SystemSoundID currentSoundID;
        NSString *path = [[NSBundle mainBundle] pathForResource:resourceName ofType:type];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:path]), &currentSoundID);
        [self playAlert:currentSoundID];
    }
}

//播放声音
+ (void)playSound:(SystemSoundID)soundID{
    AudioServicesPlaySystemSound (soundID);    // 播放
    AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
        AudioServicesDisposeSystemSoundID(soundID);    //释放声音对象以及相关的所有资源
    });
}

//播放震动
+ (void)playAlert:(SystemSoundID)soundID{
    AudioServicesPlayAlertSound (soundID);    // 播放
    AudioServicesPlaySystemSoundWithCompletion(soundID, ^{
        AudioServicesDisposeSystemSoundID(soundID);    //释放声音对象以及相关的所有资源
    });
}

- (void)play{
    AudioServicesPlaySystemSound (_soundID);// 添加摇动声音
}

- (void)dealloc{
    AudioServicesDisposeSystemSoundID(_soundID);
}
@end
